Page({
  data: {
    goods: [1062, 1176, 1206, 1248, 1260, 1374, 1416, 1428],
    extra_good: null,
    total_amount: 73304
  },

  calculate: function (e) {
    let app = getApp();
    app.goods = this.data.goods;
    app.total_amount = parseInt(this.data.total_amount);
    wx.navigateTo({
      url: '/pages/cal_result/cal_result', 
    })
  },

  change_good: function (e) {
    let index = e.currentTarget.dataset.index;
    let value = parseInt(e.detail.value);
    let goods = this.data.goods;
    goods[index] = value;
    this.setData({goods: goods});
  },

  del_good: function (e) {
    let index = e.currentTarget.dataset.index;
    let goods = this.data.goods;
    goods.splice(index, 1)
    this.setData({goods: goods})
  },

  add_good: function (e) {
    if(this.data.extra_good){
      let goods = this.data.goods;
      let new_good = parseInt(this.data.extra_good);
      if(new_good){
        goods.push(new_good);
      }
      this.setData({
        goods: goods,
        extra_good: null
      })
    }
  },

  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  bindReplaceInput: function (e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    var left
    if (pos !== -1) {
      // 光标在中间
      left = e.detail.value.slice(0, pos)
      // 计算光标的位置
      pos = left.replace(/11/g, '2').length
    }

    // 直接返回对象，可以对输入进行过滤处理，同时可以控制光标的位置
    return {
      value: value.replace(/11/g, '2'),
      cursor: pos
    }

    // 或者直接返回字符串,光标在最后边
    // return value.replace(/11/g,'2'),
  },
  bindHideKeyboard: function (e) {
    if (e.detail.value === '123') {
      // 收起键盘
      wx.hideKeyboard()
    }
  }
})
